/**
 * jims
 */
package com.jims.master.modules.drug.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.drew.lang.BufferReader;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.drug.entity.DrugSupplierCata;
import com.jims.master.modules.drug.service.DrugSupplierCataService;

import java.io.*;
import java.net.URLDecoder;
import java.util.List;

/**
 * 药品厂商Controller
 * @author DT
 * @version 2017-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugSupplierCata")
public class DrugSupplierCataController extends BaseController {

	@Autowired
	private DrugSupplierCataService drugSupplierCataService;

	/**
	 * 根据ID获取并返回对象
	 * @param id
	 * @return
	 */
	@ModelAttribute
	public DrugSupplierCata get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return drugSupplierCataService.get(id);
		}
		else {
			return new DrugSupplierCata();
		}
	}

	/**
	 * 获取列表
	 * @param drugSupplierCata
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugSupplierCata drugSupplierCata, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugSupplierCata> page = drugSupplierCataService.findPage(new Page<DrugSupplierCata>(request, response), drugSupplierCata);
		model.addAttribute("page", page);
		return "modules/drug/drugSupplierCataIndex";
	}

	/**
	 * 添加和编辑跳转
	 * @param drugSupplierCata
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugSupplierCata drugSupplierCata, Model model) {
		model.addAttribute("drugSupplierCata", drugSupplierCata);
		return "modules/drug/drugSupplierCataForm";
	}

	/**
	 * 数据更新与保存
	 * @param drugSupplierCata
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugSupplierCata drugSupplierCata, Model model, HttpServletRequest request, HttpServletResponse response)throws IOException{
		List<DrugSupplierCata> list=drugSupplierCataService.getListBySupplierName(drugSupplierCata.getSupplierName(),drugSupplierCata.getId());
		int i=0;
		StringData data=new StringData();
		if(list.size()==0){
			drugSupplierCata.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(drugSupplierCata.getSupplierJc()));
			i=drugSupplierCataService.save(drugSupplierCata);
			if(i>0){
				data.setCode("success");
				data.setData("保存成功");
			}else{
				data.setCode("error");
				data.setData("保存失败");
			}
		}
		else{
			data.setCode("info");
			data.setData("该厂商已存在，不能重复添加");
		}

		return data;
	}

	/**
	 * 数据删除
	 * @param drugSupplierCata
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugSupplierCata drugSupplierCata) {
		StringData data=new StringData();
			int i=0;
			i=drugSupplierCataService.delete(drugSupplierCata);
			if(i>0){
				data.setCode("success");
				data.setData("删除成功");
			}else{
				data.setCode("error");
				data.setData("删除失败");
			}
		return data;
	}

	/**
	 * @param drugSupplierCata,q 传递参数
	 * @return List<DrugSupplierCata>    返回类型
	 * @throws
	 * @Title: autoComplete
	 * @Desciption: (药品基本信息添加厂商自动补全)
	 * @author CTQ
	 * @date 2017-03-02
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"autoComplete", ""})
	@ResponseBody
	public List<DrugSupplierCata> autoComplete(String q, String limit,DrugSupplierCata drugSupplierCata) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<DrugSupplierCata> list = drugSupplierCataService.findAutoCompleteList(drugSupplierCata,q,limit,drugSupplierCata.getSupplierClass());
		return list;
	}

}